﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NXDO.Data.Factory;
using NXDO.WebAPI.Controllers;
using System;
using System.Collections.Generic;
using TDM.DbData.DataEntity;

namespace TDM.WebSvr.Controllers.Base
{
    /// <summary>
    /// 能量分布基础配置
    /// </summary>
    [AllowAnonymous]
    public class BaseEnergyDistributionController : ApiDbEntityController<BaseEnergyDistribution, long>
    {

        protected override string GetPkName()
        {
            return "BEDID";
        }

        protected override string GetTableName()
        {
            return "BaseEnergyDistribution";
        }
        public override BaseEnergyDistribution Post([FromBody] BaseEnergyDistribution data)
        {
            //获取用户信息
            var user = HttpContext.User as NXDO.Data.Security.Principal.MemberPrincipal;
            long uid = Convert.ToInt64(user?.MemberId);
            if (uid == 0) throw new Exception("未获得登录信息");
            data.CreateTime = DateTime.Now;
            data.CreateUID = uid;
            return base.Post(data);
        }

        public override List<BaseEnergyDistribution> PostCollection([FromBody] List<BaseEnergyDistribution> datas)
        {

            //获取用户信息
            var user = HttpContext.User as NXDO.Data.Security.Principal.MemberPrincipal;
            long uid = Convert.ToInt64(user?.MemberId);
            if (uid == 0) throw new Exception("未获得登录信息");
            datas.ForEach(t =>
            {
                t.CreateTime = DateTime.Now;
                t.CreateUID = uid;
            });
            return base.PostCollection(datas);
        }

        public override bool Delete(long pkValue)
        {
            var sql = $"update {GetTableName()} set IsDelete = 1 where {GetPkName()} = {pkValue}";
            return DbHelper.SetUpdateCommand(sql).ExecuteNonQuery() > 0;
        }

        public override List<bool> DeleteCollection([FromBody] List<long> pkValues)
        {
            List<bool> flags = new List<bool>();
            foreach (var pkValue in pkValues)
            {
                var sql = $"update {GetTableName()} set IsDelete = 1 where {GetPkName()} = {pkValue}";
                var flag = DbHelper.SetUpdateCommand(sql).ExecuteNonQuery() > 0;
                flags.Add(flag);
            }
            return flags;
        }
    }
}
